package cn.cocho.android.mc.comp.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import cn.cocho.android.mc.R;

/**
 * 继承ImageView的控件可以用作button按钮使用
 * 
 * @author HAIKANG SONG
 * @2013年11月11日 @上午10:24:34
 */
public class ImageStateButton extends ImageView {

	private static final String PRESSED_SRC = "pressed_src";
	private static final String DISABLE_SRC = "disable_src";

	private Drawable src;
	private Drawable pressedSrc;
	private Drawable disableSrc;

	public ImageStateButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		initComponents(context, attrs);
	}

	public ImageStateButton(Context context) {
		super(context);
	}

	public ImageStateButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initComponents(context, attrs);

	}

//	private void initComponents(Context context, AttributeSet attrs) {
//		TypedArray attrArray = context.obtainStyledAttributes(attrs,
//				R.styleable.ImageStateButton);
//		src = getDrawable();
//		pressedSrc = getDrawable(attrs, attrArray, PRESSED_SRC,
//				R.styleable.ImageStateButton_pressed_src, src);
//		disableSrc = getDrawable(attrs, attrArray, DISABLE_SRC,
//				R.styleable.ImageStateButton_disable_src, src);
//		if (!isEnabled()) {
//			setImageDrawable(disableSrc);
//		}
//	}
	
	//*↑
	private void initComponents(Context context, AttributeSet attrs) {
		TypedArray attrArray = context.obtainStyledAttributes(attrs,
				R.styleable.ImageStateButton);
		pressedSrc = attrArray.getDrawable(R.styleable.ImageStateButton_pressed_src);
		disableSrc = attrArray.getDrawable(R.styleable.ImageStateButton_disable_src);
		attrArray.recycle();
		
		if (!isEnabled()) {
			setImageDrawable(disableSrc);
		}
	}

//	private Drawable getDrawable(AttributeSet attrs, TypedArray attrArray,
//			String attrName, int styleId, Drawable defValue) {
//		int resourceId = attrs.getAttributeResourceValue(CompUtils.NS_TBC, attrName, 0);
//		if (resourceId <= 0) {
//			resourceId = attrArray.getResourceId(styleId, 0);
//		}
//
//		if (resourceId <= 0) {
//			return defValue;
//		}
//
//		Resources resources = getResources();
//		return resources.getDrawable(resourceId);
//	}


	public boolean isCancleClick(MotionEvent event) {
		return event.getX() < 0 || event.getX() > getWidth()
				|| event.getY() < 0 || event.getY() > getHeight();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (!isEnabled()) {
			return true;
		}

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setImageDrawable(pressedSrc);
			break;
		case MotionEvent.ACTION_CANCEL:
		case MotionEvent.ACTION_UP:
			setImageDrawable(src);
			if (!isCancleClick(event)) {
				performClick();
			}
		}

		return true;
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		if (enabled) {
			setImageDrawable(src);
		} else {
			setImageDrawable(disableSrc);
		}
	}

	public Drawable getPressedSrc() {
		return pressedSrc;
	}

	public void setPressedSrc(Drawable pressedSrc) {
		this.pressedSrc = pressedSrc;
	}
}
